import time
import unittest

from framework.core.crawler_result import Result
from framework.core.crawler_signal import Signal
from framework.core.crawler_task import Task
from framework.core.sqs_task_pool import SQSTaskPool


class SQSTaskPoolTestCase(unittest.TestCase):
    @unittest.skip("temp")
    def test_UniCastSignal(self):
        # Setup
        signal = Signal.create("unit test", "UniCast", "detail_type1", {"tag1": "abcdefg"})
        pool = SQSTaskPool("pt-spider-queue")
        # pool.purge()
        pool.enqueue(signal)
        signal1 = pool.dequeue()
        self.assertEqual(signal1.source, "unit test")
        self.assertEqual(signal1.kind, "UniCast")
        self.assertEqual(signal1.detail_type, "detail_type1")
        self.assertEqual(signal1.detail.get("tag1"), "abcdefg")
        time.sleep(15)
        signal2 = pool.dequeue()
        self.assertIsNone(signal2, None)

        pass

    def test_BroadCastSignal(self):
        # Setup
        signal = Signal.create("unit test", "BroadCast", "detail_type1", {"tag1": "abcdefg"})
        pool = SQSTaskPool("pt-spider-queue")
        # pool.purge()
        pool.enqueue(signal)
        signal1 = pool.dequeue()
        self.assertEqual(signal1.source, "unit test")
        self.assertEqual(signal1.kind, "BroadCast")
        self.assertEqual(signal1.detail_type, "detail_type1")
        self.assertEqual(signal1.detail.get("tag1"), "abcdefg")
        time.sleep(15)
        signal2 = pool.dequeue()
        self.assertIsNotNone(signal2, None)

        pass

    def test_TaskSerializeAndDeserialize(self):
        # Setup
        url_list = [
            ("http://www.baidu.com", {"name": "百度1", "type": "2"}, "BestSeller"),
            # ("http://www.baidu2.com", {"name": "百度2", "type": "2"}, "BestSeller"),
            # ("http://www.baidu3.com", {"name": "百度3", "type": "2"}, "BestSeller")
        ]

        pool = SQSTaskPool("pt-spider-queue")
        result = Result.create(source_url="http://www.baidu.com", result_type="BestSeller_Department", departmentName={"department": "A"}, name="ABC")
        tasks = Task.create_tasks(url_list, parser='default', parent_result=result)

        # Execution
        pool.enqueue(tasks)

        task1 = pool.dequeue()
        task2 = pool.dequeue()
        # task3 = pool.dequeue()

        # Assert
        self.assertEqual(task1.url, tasks[0].url)
        self.assertEqual(task1.metadata, tasks[0].metadata)
        self.assertEqual(task1.parser, tasks[0].parser)
        self.assertEqual(task1.desc, tasks[0].desc)
        for k, v in task1.tags.items():
            self.assertEqual(task1.tags[k], tasks[0].tags[k])
        self.assertEqual(task1.parent_result.source_url, tasks[0].parent_result.source_url)
        self.assertEqual(task1.parent_result.metadata, tasks[0].parent_result.metadata)
        for k, v in task1.parent_result.items.items():
            self.assertEqual(task1.parent_result.items[k], tasks[0].parent_result.items[k])
        for k, v in task1.parent_result.tags.items():
            self.assertEqual(task1.parent_result.tags[k], tasks[0].parent_result.tags[k])

        self.assertIsNone(task2)
        # self.assertIsNotNone(task3)
        pass


if __name__ == '__main__':
    unittest.main()
